// mircoApp/gradevin/cargoWayManage/cargoWayManage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgPath: app.globalData.imgPath,
    show: false,
    searchVal: "",
    action: [], //api数组
    record: [],
    searchGood: [], //搜索的货道商品
    selectGoodId: "",
    cargoId: "",

    page: 1,
    isLoadMore: true,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
    this.getData();
  },
  init() {
    this.setData({
      isLoadMore: true,
      total_page: 1,
      action: [],
      record: [],
      show: false,
      searchVal: "",
      searchGood: [],
      cargoId: ""
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  closeShow() {
    this.setData({
      searchGood: [],
      selectGoodId: '',
      show: false
    })
  },
  toSearch() {
    this.setData({
      searchGood: []
    })
    this.searchGoodBox();
  },
  // 加载更多
  tapMore() {
    this.searchGoodBox("", "more");
  },
  selectGoodTap(e) {
    let {
      gid
    } = e.currentTarget.dataset;
    this.setData({
      selectGoodId: gid,
      show: false,
      searchGood: []
    })
    this.addCargo();
  },
  // 货道增加商品列表
  addCargo() {
    let urlStr = app.getNetAddresss("plugin.gradevin.frontend.owner.cargo-road.change");
    app._postNetWork({
      url: urlStr,
      data: {
        id: this.data.cargoId,
        goods_id: this.data.selectGoodId,
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.init();
          this.getData();
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
  searchGoodBox(e, key = '', id = '') {
    if (e) {
      let {
        cid
      } = e.currentTarget.dataset;
      if (cid) {
        this.setData({
          cargoId: cid
        })
      }
    }
    let json = {
      title: this.data.searchVal
    };
    console.log(this.data.cargoId, 'ddddd  cargoId  ddddd')
    if (key == "more") {
      if (this.data.searchGood.current_page >= this.data.searchGood.last_page) {
        console.log("没有更多了");
        return;
      }
      json.page = this.data.searchGood.current_page + 1;
    }

    let urlStr = app.getNetAddresss("plugin.gradevin.frontend.owner.cargo-road.goods");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          console.log(res.data)
          this.setData({
            show: true
          })
          if (key == 'more') {
            this.data.searchGood.data = this.data.searchGood.data.concat(res.data.data);
            this.data.searchGood.current_page = json.page;
            this.setData({
              searchGood: this.data.searchGood
            })
          } else {
            this.setData({
              searchGood: res.data
            })
          }
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
  cleanCargo(e) {
    let {
      id
    } = e.currentTarget.dataset;
    let urlStr = app.getNetAddresss("plugin.gradevin.frontend.owner.cargo-road.clear");
    app._postNetWork({
      url: urlStr,
      data: {
        id
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.init();
          this.getData();
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
  openCargo(e){
    let {
      id
    } = e.currentTarget.dataset;
    let urlStr = app.getNetAddresss("plugin.gradevin.frontend.owner.cargo-road.open");
    app._postNetWork({
      url: urlStr,
      data: {
        id
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          app.tips(res.msg);
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {}
    });
  },
  cargoChange(json) {
    let urlStr = app.getNetAddresss("plugin.gradevin.frontend.owner.cargo-road.status");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.init();
          this.getData();
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
  cargoUpdate(e) {
    let {
      key,
      item
    } = e.currentTarget.dataset;
    let json = {
      status: 0,
      id: item.id
    };
    if (key == 'up') {
      json.status = 1;
      this.cargoChange(json)
    } else if (key == 'down') {
      wx.showModal({
        title: '是否确认下架',
        complete: (res) => {
          if (res.confirm) {
            this.cargoChange(json)
          }
        }
      })
    }
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.gradevin.frontend.owner.cargo-road.index");
    app._postNetWork({
      url: urlStr,
      data: {
        device_id: this.options.device_id,
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            action: res.data.action,
            record: res.data.record.data,
          })
          console.log(res.data, 'dddddddd')
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  }
})